<template>
	<view class="materialsNotList">
		<!-- 材料未还 -->
		<navBar title="材料归还" goBackUrl="/pages/eventArchiving/eventArchivingList"  @goBack="goBack"></navBar>
		
		<view class="bgc"></view>
		
		<scroll-view scroll-y="true" class="scroll-box">
			<view class="content">
				<view class="list" v-for="(item,index) in materialList" :key="item.name">
					<view class="title">
						<view class="name">{{ item.name}}</view>
						<view class="value">{{ item.totalNum }}</view>
					</view>
					<view class="coll" v-for="(value, idx2) in item.children" :key="idx2">
						<view class="coll-item">
							<view class="name">规格型号： </view>
							<view class="value">{{ value.specificationModel || "" }}</view>
						</view>
						<view class="coll-item">
							<view class="name">出库数量： </view>
							<view class="value">{{ value.exNum2 }}{{ value.unit }}</view>
						</view>
						<view class="coll-item">
							<view class="name">所属基地： </view>
							<view class="value">{{ value.stationName }}</view>
						</view>
						<view class="coll-item coll-item2">
							<view class="name">归还数量： </view>
							<view class="value">
								<u-number-box v-model="value.backNum" :min="0" :step="!value.backNum ? value.exNum2 : 1" :max="value.exNum2"  color="#323233"></u-number-box> <text class="unit">{{ value.unit }} </text>
							</view> 
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<view class="fixedButton">
			<u-button type="primary"  :loading="loading" text="提交" @click="submit"></u-button>
		</view>
	</view>
</template>

<script>
	import { getMaterialOutCode, getMaterialBackSaveEx } from "@/api/materials/materials.js"
	export default{
		name: 'materialsNotList',
		data() {
			return {
				loading: false,
				materialList: [],
				data: {},
			}
		},
		onLoad(option) {
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('setData', (data) => {
				console.log("setDatasetDatasetData", data.data)
				this.data = data.data
				this.getReturnList(this.data.materialExWarehouseCode)
			})
		},
		methods: {
			// 获取要归还的列表
			async getReturnList(code){
				// 获取材料归还列表
				let res = await getMaterialOutCode({code: code})
				let list = res.result.map(item=>({...item, backNum: 0, exNum2: item.exNum.toFixed(2)}))
				this.materialList = this.arrayFilter(list, "name") 
				console.log(this.materialList);
			},
			
			// 提交
			async submit(){
				if(this.loading) return
				this.loading = true
	
				let list = []
				this.materialList.forEach(item=>{
					item.children.forEach(value=>{
						list.push(value)
					})
				})
				console.log(list);
				let projectCascade = this.data.projectCascade
				let materiaNames = []
				let materialList = []
	
				list.forEach(item=>{
					materiaNames.push(item.name)
					let obj = {
						materialId: item.id,
						num: item.backNum
					}
					materialList.push(obj)
				})
				
				let _query = {
					exCode: this.data.materialExWarehouseCode,
					projectCascade: projectCascade,
					materiaNames: materiaNames.join(","),
					materialList: materialList,
				}
				console.log(_query);
				let res = await getMaterialBackSaveEx(_query)
				this.loading = false
				if(res.success){
					
					// 调用上个页面的方法
					let pages = getCurrentPages();
					let beforePage = pages[pages.length - 2];//上个页面
					// #ifdef H5
					beforePage.getList()
					// #endif
					// #ifndef H5
					beforePage.$vm.getList()
					// #endif
					
					uni.showToast({
						title: '保存成功',
						icon: "success"
					})
					this.getReturnList(this.data.materialExWarehouseCode)
					
					
					uni.navigateBack({
						url: "/pages/eventArchiving/eventArchivingList"
					})
				}
			},
			
			goBack(url){
				uni.navigateBack({
					url: url
				})
			},
			
			/**
			* @description:  list 数组根据 stationName分组 num统计数量
			* @param {type}  list: 需要处理的数组  (原数组)
			* @param {type}  stationName: 用于判断相同的键名 
			* @return:       返回值: 数组
			*/
			arrayFilter(list, stationName){
				let obj = {}
				let newList = []
				for (let i = 0; i < list.length; i++) {
					let item = list[i]
					let str = item[stationName]
					if(obj[str]){
						obj[str].push(item)
					}else{
						obj[str]=[]
						obj[str].push(item)
					}
				}
				// 对象转数组
				for (let key in obj) {
					let value = obj[key]
					let totalNum = 0
					let unit = ""
					value.forEach(item=>{
						totalNum = totalNum +  +item.exNum
						unit = item.unit
					})
					let newkey = {name: key, totalNum:totalNum+unit, children: value}
					newList.push(newkey)
				}
				return newList
			}
		}
	}
</script>

<style lang="scss" scoped>
	.materialsNotList{
		width: 100%;
		height: 100%;
		.bgc{
			width: 100%;
			height: 30rpx;
			background-color: #F5F7FA;
		}
		.scroll-box{
			height: calc(100vh - 320rpx);
			padding-top: 10rpx;
		}
		
		.content{
			height: calc(100vh - 320rpx);
			padding-bottom: 30rpx;
		}
		.list{
			padding: 0rpx 45rpx;
			background-color: #fff;
			.title{
				display: flex;
				line-height: 120rpx;
				height: 120rpx;
				align-items: center;
				justify-content: space-between;
				font-size: 32rpx;
				font-weight: 600;
				color: #445469;
				border-bottom: 2rpx solid #E2E5ED;
				.value{
					margin-right: 25rpx;
				}
			}
			.coll{
				padding-bottom: 20rpx;
				border-bottom: 2rpx solid #E2E5ED;
				.coll-item{
					padding: 0rpx 20rpx;
					margin-top: 20rpx;
					display: flex;
					align-items: center;
					
					.name{
						margin-left: 25rpx;
					}
				}
				.coll-item2{
					justify-content: space-between;
					.value{
						display: flex;
						align-items: center;
					}
				}
			}
		}
	}
</style>
<style>
	page{
		height: 100%;
		width: 100%;
	}
	body{
		width: 100%;
		height: 100%;
	}
</style>